package com.jstyle.jclife.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jstyle.jclife.R;
import com.jstyle.jclife.utils.ScreenUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/27.
 */

public class BpCalibrationDialog extends JstyleDialog {


    @BindView(R.id.iv_calibration_tips)
    ImageView ivCalibrationTips;
    @BindView(R.id.tv_calibration_tips)
    TextView tvCalibrationTips;
    @BindView(R.id.bt_calibration_confim)
    Button btCalibrationConfim;
    @BindView(R.id.tv_measure_title)
    TextView tvMeasureTitle;
    @BindView(R.id.iv_calibration_anim)
    ImageView ivCalibrationAnim;
    @BindView(R.id.iv_calibration_result)
    ImageView ivCalibrationResult;
    @BindView(R.id.tv_calibration_failed_reason)
    TextView tvCalibrationFailedReason;
    @BindView(R.id.tv_calibration_result)
    TextView tvCalibrationResult;
    boolean isFinishCalibration;
    int finishCode;
    int complete= 13;
    private AnimationDrawable animationDrawable;
    CalibrationResultListener calibrationResultListener;

    public BpCalibrationDialog(@NonNull Context context) {
        super(context);

    }

    public BpCalibrationDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context, themeResId);
    }

    private void init(Context context, int themeResId) {
        initStyle(themeResId);
        initWidth(ScreenUtils.getScreenWidth(context));
        initGravity(Gravity.CENTER);
    }


    @Override
    protected void setContentView() {
        setContentView(R.layout.dialog_bpcalibration);
    }


    @OnClick(R.id.bt_calibration_confim)
    public void onViewClicked() {
        if (isFinishCalibration) {
            dismiss();
            if(calibrationResultListener!=null){
                if(finishCode==complete){
                    calibrationResultListener.onCalibrationSuccess();
                }else {
                    calibrationResultListener.onCalibrationFailed();
                }
            }
        } else {
            ivCalibrationTips.setVisibility(View.GONE);
            tvCalibrationTips.setVisibility(View.GONE);
            btCalibrationConfim.setVisibility(View.GONE);
            ivCalibrationAnim.setVisibility(View.VISIBLE);
            tvMeasureTitle.setVisibility(View.VISIBLE);
            animationDrawable = (AnimationDrawable) ivCalibrationAnim.getDrawable();
            animationDrawable.start();
        }

    }

    public void setBpCalibrationFinish(int code) {

        this.isFinishCalibration = true;
        this.finishCode=code;
        if (animationDrawable != null) animationDrawable.stop();
        ivCalibrationAnim.setVisibility(View.GONE);
        tvMeasureTitle.setVisibility(View.GONE);
        btCalibrationConfim.setVisibility(View.VISIBLE);
        tvCalibrationResult.setVisibility(View.VISIBLE);
        ivCalibrationResult.setVisibility(View.VISIBLE);
        if (code == 14||code==6) {
            tvCalibrationResult.setText("Calibration failed!");
            tvCalibrationFailedReason.setVisibility(View.VISIBLE);
        }
    }

    public void setCalibrationListener(CalibrationResultListener calibrationListener) {
        this.calibrationResultListener = calibrationListener;
    }

    public interface CalibrationResultListener {
        public void onCalibrationSuccess();

        public void onCalibrationFailed();
    }
}
